package de.eeb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.unikoeln.sharedbucks.R;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;
import de.eeb.common.ContactParcel;
import de.eeb.common.SBException;

public class PickPeopleActivity extends Activity {

	public Map<String, SBContact> sbContactMap = new HashMap<String, SBContact>();

	private void readContacts() throws SBException {
		Cursor people = getContentResolver().query(
				ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

		Map<String, SBContact> tmpMap = new HashMap<String, SBContact>();

		List<String> phoneNumberList = new ArrayList<String>();

		while (people.moveToNext()) {
			int nameFieldColumnIndex = people
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
			String contact = people.getString(nameFieldColumnIndex);

			String contactId = people.getString(people
					.getColumnIndex(ContactsContract.Contacts._ID));

			Cursor phones = getContentResolver().query(Phone.CONTENT_URI, null,
					Phone.CONTACT_ID + " = " + contactId, null, null);
			while (phones.moveToNext()) {
				String number = phones.getString(phones
						.getColumnIndex(Phone.NUMBER));

				number = number.replaceAll("\\s", "");

				int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));

				if (type == Phone.TYPE_MOBILE) {
					phoneNumberList.add(number);
					tmpMap.put(number, new SBContact(number, contact, null));
					break;
				}
			}
			phones.close();

		}

		List<String> webServiceResultList = MainViewActivity.SBController
				.getFellowUsers(phoneNumberList);

		for (String s : phoneNumberList) {
			SBContact contact = tmpMap.get(s);
			String hash = contact.getHash();
			if (webServiceResultList.contains(hash))
				sbContactMap.put(s, contact);
		}

		people.close();

	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pick_people_layout);

		try {
			readContacts();
		} catch (SBException e) {
			Log.e(this.getClass().getName(), e.getMessage());
			throw new RuntimeException("SBError: Cannot connect to Web Service");
		}

		Log.d(this.getClass().getName(), "Found " + sbContactMap.size()
				+ "numbers");

		TableLayout tl = (TableLayout) findViewById(R.id.PickPeopleView);

		for (String key : sbContactMap.keySet()) {

			SBContact contact = sbContactMap.get(key);

			LayoutParams layoutParams = new TableLayout.LayoutParams(
					TableLayout.LayoutParams.MATCH_PARENT,
					TableLayout.LayoutParams.WRAP_CONTENT);

			TableRow tr = new TableRow(PickPeopleActivity.this);
			tr.setLayoutParams(layoutParams);

			TextView nameTextEdit = new TextView(PickPeopleActivity.this);
			nameTextEdit.setText(contact.getName());
			tr.addView(nameTextEdit);

			CheckBox cb = new CheckBox(PickPeopleActivity.this);
			cb.setSelected(false);
			tr.addView(cb);
			contact.setCb(cb);

			tl.addView(tr);
		}
	}

	public void okButtonClicked(View view) {
		Intent data = new Intent();
		
		ArrayList<ContactParcel> parcelList = new ArrayList<ContactParcel>();
		
		Log.v(this.getClass().getName(), "Activity result...");
		
		for(String s : sbContactMap.keySet()) {
			SBContact contact = sbContactMap.get(s);
			boolean isParticipating = contact.getCb().isChecked();
			ContactParcel parcel = new ContactParcel(	contact.getName(), 
														contact.getNumber(), 
														contact.getHash(), 
														isParticipating ? 1 : 0);
			
			if(isParticipating) {
				parcelList.add(parcel);
			}
		}
		
		data.putParcelableArrayListExtra("contact_list", parcelList);
		
		setResult(RESULT_OK, data);
		finish();
	}

}
